<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Pay;
use app\common\model\Banner;
use app\common\model\Cart;
use app\common\model\Category;
use app\common\model\product\Member;
use app\common\model\Goods;
use app\common\model\product\Order;
use think\Db;
use think\Queue;

/**
 * 产品会员接口
 * @ApiInternal
 */
class Product extends Api
{
    protected $noNeedLogin = ['index'];
    protected $noNeedRight = ['*'];

    /**
     * 产品会员信息
     *
     */
    public function index()
    {
        $data['rules'] = config('site.product_rules');
        $data['s_introduction'] = config('site.p_introduction');
        $data['s_agreement'] = config('site.s_agreement');
        $data['category'] = Category::all(['type' => 'product']);
        $data['goodsList'] = Member::getProductMemberList($this->auth->id);
        $this->success('', $data);
    }
    /**
     * 订单下单预览
     *
     */
    public function yuOrder()
    {
        $get = request()->get();
        $uid =  $this->auth->id;
        extract($get);
        $order = new Order();
        $data = $order->yuOrder($goods_id);
        $data['pay_type'] = $pay_type ?? 0;
        cache('product_' . $uid, $data, '900');
        $this->success('', $data);
    }
    /**
     * 订单统一下单
     *
     */
    public function addOrder()
    {
        if (!$this->request->isPost()) {
            $this->error('提交类型错误');
        }
        $uid = $this->auth->id;
        $data = cache('product_' . $uid);
        if(!$data){
            $this->error('请重新下单');
        }
        extract($data);
        if (!$data) {
            $this->error('订单已失效，请重新选择商品');
        }
        if (!isset($pay_type)) {
            $this->error('请选择支付方式');
        }
        Db::startTrans();
        try {
            //添加订单商品信息
            $order = new Order();
            $order_id = $order->addData($data, $uid);
            if (!$order_id) {
                exception('下单失败');
            }
            $total_order = Order::get($order_id);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return self::error($e->getMessage());
        }
        Queue::later(900, 'app\common\job\Product', $total_order, 'product');
        cache('product_' . $uid, null);
        return $this->success('下单成功', $order_id);
    }
    /**
     * 立即付款
     *
     */
    public function pay()
    {
        if (!$this->request->isPost()) {
            $this->error('提交类型错误');
        }
        $post = $this->request->post();
        extract($post);
        if (!isset($id) || empty($id)) {
            $this->error('参数错误');
        }
        try {
            $order = Order::get($id);
            if ($order['price'] <= 0) {
                exception('支付失败');
            } else {
                $data['title'] = $order['name'];
                $data['sn'] = $order['sn'];
                $data['pay_money'] = $order['price'];
                $data['user_id'] = $order['user_id'];
                if ($order['pay_type'] == '0') {
                    $sdk = Pay::wxpay($data, 'order');
                } elseif ($order['pay_type'] == '1') {
                    $sdk = Pay::alipay($data);
                } else {
                    //余额支付
                    $sdk =  Pay::yue($data);
                }
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('', $sdk);
    }

    /**
     * 会员开通记录
     *
     */
    public function openingRecord()
    {
        $order = Order::where(['user_id' => $this->auth->id, 'status' => 1])->order('id desc')->select();
        $this->success('', $order);
    }
}
